import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jyofservice/delay/DelayPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:jyofservice/fluro/NavigatorUtil.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:jyofservice/util/SlideRoute.dart';
import 'package:jyofservice/tonghuajilu/CallRecord.dart';
import 'package:jyofservice/tongjichaxun/TongJiChaXun.dart';
import 'package:jyofservice/zuoxijianguan/SeatCharge.dart';
import 'package:jyofservice/gongdandiaodu/GongDanDiaoDu.dart';
import 'package:jyofservice/kaoqin/SignList.dart';
import 'package:jyofservice/jicha/JiChaInfo.dart';

class LeaderPage extends StatefulWidget {
  LeaderPage({Key? key}) : super(key: key);
  @override
  LeaderPageState createState() => LeaderPageState();
}

class LeaderPageState extends State<LeaderPage> {
  int? incomingNum = 0;
  int? sendOrderNum = 0;
  int? consumeNum = 0;
  int? seatNum = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getStaticNum();
  }

  @override
  Widget build(BuildContext context) {

    return Column(
      children: <Widget>[
//        Container(
//            color: Colors.white,
//            height: ScreenUtil().setHeight(250),
//            child: GridView.count(
//                scrollDirection: Axis.horizontal,
//                crossAxisCount: 1,
//              children: <Widget>[
//                centerColumn("images/gongdanchaxun.png", "工单查询"),
//                centerColumn("images/delay.png", "缓办审批"),
//                centerColumn("images/tognjichaxun.png", "统计查询"),
//                centerColumn("images/zuoxizhongtaijianguan.png", "坐席监管"),
//                centerColumn("images/tonghuajilu.png", "通话记录"),
//              ],
//
//            ),
//
//        ),
//        SizedBox(
//          height: ScreenUtil().setHeight(20),
//        ),
        Container(
          height: ScreenUtil().setHeight(600),
          child: GridView.count(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            crossAxisCount: 2,
            crossAxisSpacing: 1,
            mainAxisSpacing: 1,
            childAspectRatio: 2,
            children: <Widget>[
              bottomSection("images/liubianxing_92.png", 34, 142, 246, "今日来电量",
                  incomingNum.toString() + "个"),
              bottomSection("images/liubianxing_86.png", 57, 201, 114, "今日派单量",
                  sendOrderNum.toString() + "个"),
              bottomSection("images/liubianxing_90.png", 255, 163, 104, "今日呼损量",
                  consumeNum.toString() + "个"),
              bottomSection("images/liubianxing_93.png", 254, 142, 142, "座席数",
                  seatNum.toString() + "个"),
            ],
          ),
        )
      ],
    );
  }

  Container bottomSection(
      String assets, int r, int g, int b, String title, String num) {
    return Container(
      padding: const EdgeInsets.only(top: 10),
      color: Colors.white,
      child: ListTile(
        title: Text(
          title,
          style: TextStyle(
            fontSize: ScreenUtil().setSp(30),
            color: Colors.black54,
          ),
        ),
        subtitle: Text(
          num,
          style: TextStyle(
              fontStyle: FontStyle.italic,
              color: Color.fromRGBO(r, g, b, 1),
              fontSize: ScreenUtil().setSp(40),
              fontWeight: FontWeight.bold),
        ),
        leading: Image.asset(assets),
      ),
    );
  }

  TextButton centerColumn(String asset, String label) {
    return TextButton(
      onPressed: () {
        if (label == "通话记录") {
//          NavigatorUtil.goCallRecord(context);
          Navigator.push(context, SlideRoute().createRoute(CallRecordPage()));
        } else if (label == "统计查询") {
//          NavigatorUtil.goTongJiChaXun(context);
          Navigator.push(context, SlideRoute().createRoute(TongJiChaXunPage()));
        } else if (label == "坐席监管") {
//          NavigatorUtil.goSeatCharge(context);
          Navigator.push(context, SlideRoute().createRoute(SeatChargePage()));
        } else if (label == "工单查询") {
//          NavigatorUtil.goGongDanDiaoDu(
//              context, "GetMyAndChildrenAllWorkForm", "工单查询");
          Navigator.push(context, SlideRoute().createRoute(GongDanDiaoDuPage(view:"GetMyAndChildrenAllWorkForm",title:"工单查询")));
        } else if (label == "签到") {
//          NavigatorUtil.goSign(context);
//           Navigator.push(context, SlideRoute().createRoute(SignList()));

        } else if(label=="稽查信息") {
//          NavigatorUtil.goJiCha(context);
          Navigator.push(context, SlideRoute().createRoute(JiChaInfo()));
        }
        else if(label=="缓办审批") {
//          NavigatorUtil.goJiCha(context);
          Navigator.push(context, SlideRoute().createRoute(DelayPage()));
        }
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            asset,
            width: ScreenUtil().setWidth(140),
            height: ScreenUtil().setHeight(140),
          ),
          SizedBox(
            height: ScreenUtil().setHeight(5),
          ),
          Text(
            label,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30),
              color: Colors.black,
            ),
          )
        ],
      ),
    );
  }

  Future getStaticNum() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    String? key = sharedPreferences.getString("key");
    String aesData= await AesUtil.encodeBase64();
    var data = {"key": key,"ClientType":"phone","ClientKey":aesData,"type":"day"};
    Response response = await Dio().post(
        WebConfig().baseUrl +
            "Statistics/GetMyDepartmentPhoneInfoForHomePage",
        data: data);
    print("统计==${response.data.toString()}");
    if (response.data["Code"] == "200") {
      if (mounted) {
        setState(() {
          print(response.data["Data"][0]["a"]);
          seatNum = response.data["Data"][0]["a"];
          incomingNum = response.data["Data"][2]["a"];
          sendOrderNum = response.data["Data"][5]["a"];
          consumeNum = response.data["Data"][7]["a"];
        });
      }
    }
  }
}
